package org.goyo.main.services.security;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.goyo.common.exception.DBException;
import org.goyo.main.domain.dao.system.ISysUserDao;
import org.goyo.main.domain.dao.system.ISysUserRoleDao;
import org.goyo.main.domain.entity.system.SysUser;
import org.goyo.main.domain.entity.system.SysUserRole;
import org.goyo.main.web.controller.system.SessionUtil;
import org.springframework.security.authentication.encoding.PasswordEncoder;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestContextHolder;
@Transactional
public class UserDetailServiceImpl implements UserDetailsService {
	
	private ISysUserDao sysUserDao;
	private ISysUserRoleDao sysUserRoleDao;
	private PasswordEncoder passwordEncoder;
	
	public UserDetails loadUserByUsername(String userName)
			throws UsernameNotFoundException {
		
//		//初始化	
//		SysUser user = new SysUser();
//		user.setUserName("admin");
//		String password = null;
//		password = passwordEncoder.encodePassword("123456", null);
//		user.setPassword(password);
//		sysUserDao.save(user);
		SysUser sysuser = null;
		try {
			if("administrator".equals(userName)){
				sysuser = SysUser.getSupplier();
			}else{
				sysuser = sysUserDao.findByUserName(userName);	
			}
			
		} catch (DBException e) {
			e.printStackTrace();
		}
		if(sysuser == null)
			throw new UsernameNotFoundException("用户不存在!");
		sysuser.setAccountNonExpired(true);
		sysuser.setAccountNonLocked(true);
		sysuser.setEnabled(true);
		sysuser.setCredentialsNonExpired(true);
		Set<GrantedAuthority> authorities = getAuthorities(sysuser);
		sysuser.setAuthorities(authorities);
		return sysuser;
	}
	/**
	 * 获得用户权限
	 * @return
	 */
	public Set<GrantedAuthority> getAuthorities(SysUser sysuser){
		Set<GrantedAuthority> authSet = new HashSet<GrantedAuthority>();
		List<SysUserRole> sysUserRoles = getSysUserRole(sysuser);
		if(sysUserRoles == null ){
			SessionUtil.setAttribute("user", sysuser);
			authSet.add(new GrantedAuthorityImpl("GOYO"));
			return authSet;
		}
			
		//查询资源角色表
		for(SysUserRole  sysUserRole : sysUserRoles){
			authSet.add(new GrantedAuthorityImpl(sysUserRole.getRoleId()));
		}
//		String session = RequestContextHolder.currentRequestAttributes().getSessionId();
		SessionUtil.setAttribute("user", sysuser);
		authSet.add(new GrantedAuthorityImpl(sysuser.getUuid()));
		return authSet;
	}
	
	/**
	 * session
	 * @param sysuser
	 * @return
	 */
	public List<SysUserRole> getSysUserRole(SysUser sysuser){
		Object obj = SessionUtil.getAttribute(sysuser.getUuid());
		if(sysuser.isSupper()){
			return null;
		}
		if(obj == null){
			try {
				List<SysUserRole> sysUserRoles =  sysUserRoleDao.findByUserId(sysuser.getUuid());
				SessionUtil.setAttribute(sysuser.getUuid(), sysUserRoles);
				return sysUserRoles;
			} catch (DBException e) {
				e.printStackTrace();
			}
		}
		return (List<SysUserRole>) obj;
	}

	public PasswordEncoder getPasswordEncoder() {
		return passwordEncoder;
	}

	public void setPasswordEncoder(PasswordEncoder passwordEncoder) {
		this.passwordEncoder = passwordEncoder;
	}

	public ISysUserDao getSysUserDao() {
		return sysUserDao;
	}

	public void setSysUserDao(ISysUserDao sysUserDao) {
		this.sysUserDao = sysUserDao;
	}

	public ISysUserRoleDao getSysUserRoleDao() {
		return sysUserRoleDao;
	}

	public void setSysUserRoleDao(ISysUserRoleDao sysUserRoleDao) {
		this.sysUserRoleDao = sysUserRoleDao;
	}

	
}
